home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / File / Passwd / Cvs.php < prev    next >
PHP Script  |  2004-10-01  |  9KB  |  295 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: File :: Passwd :: Cvs                                        |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at>             |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: Cvs.php,v 1.12 2004/06/07 19:19:47 mike Exp $
  15.  
  16. /**
  17. * Manipulate CVS pserver passwd files.
  18. * @author   Michael Wallner <mike@php.net>
  19. * @package  File_Passwd
  20. */
  21.  
  22. /**
  23. * Requires File::Passwd::Common
  24. */
  25. require_once 'File/Passwd/Common.php';
  26.  
  27. /**
  28. * Manipulate CVS pserver passwd files.
  29. * <kbd><u>
  30. *   A line of a CVS pserver passwd file consists of 2 to 3 colums:
  31. * </u></kbd>
  32. * <pre>
  33. *   user1:1HCoDDWxK9tbM:sys_user1
  34. *   user2:0O0DYYdzjCVxs
  35. *   user3:MIW9UUoifhqRo:sys_user2
  36. * </pre>
  37. * If the third column is specified, the CVS user named in the first column is 
  38. * mapped to the corresponding system user named in the third column.
  39. * That doesn't really affect us - just for your interest :)
  40. * <kbd><u>Output of listUser()</u></kbd>
  41. * <pre>
  42. *      array
  43. *       + user =>  array
  44. *                   + passwd => crypted_passwd
  45. *                   + system => system_user
  46. *       + user =>  array
  47. *                   + passwd => crypted_passwd
  48. *                   + system => system_user
  49. * </pre>
  50. * @author   Michael Wallner <mike@php.net>
  51. * @package  File_Passwd
  52. * @version  $Revision: 1.12 $
  53. * @access   public
  54. */
  55. class File_Passwd_Cvs extends File_Passwd_Common
  56. {
  57.     /**
  58.     * Constructor
  59.     *
  60.     * @access public
  61.     */
  62.     function File_Passwd_Cvs($file = 'passwd')
  63.     {
  64.         parent::__construct($file);
  65.     }
  66.     
  67.     /**
  68.     * Fast authentication of a certain user
  69.     * 
  70.     * Returns a PEAR_Error if:
  71.     *   o file doesn't exist
  72.     *   o file couldn't be opened in read mode
  73.     *   o file couldn't be locked exclusively
  74.     *   o file couldn't be unlocked (only if auth fails)
  75.     *   o file couldn't be closed (only if auth fails)
  76.     *
  77.     * @static   call this method statically for a reasonable fast authentication
  78.     * @access   public
  79.     * @return   mixed   true if authenticated, false if not or PEAR_Error
  80.     * @param    string  $file   path to passwd file
  81.     * @param    string  $user   user to authenticate
  82.     * @param    string  $pass   plaintext password
  83.     */
  84.     function staticAuth($file, $user, $pass)
  85.     {
  86.         $line = File_Passwd_Common::_auth($file, $user);
  87.         if (!$line || PEAR::isError($line)) {
  88.             return $line;
  89.         }
  90.         @list(,$real)   = explode(':', $line);
  91.         return (File_Passwd_Cvs::generatePassword($pass, $real) === $real);
  92.     }
  93.     
  94.     /**
  95.     * Apply changes and rewrite CVS passwd file
  96.     *
  97.     * Returns a PEAR_Error if:
  98.     *   o directory in which the file should reside couldn't be created
  99.     *   o file couldn't be opened in write mode
  100.     *   o file couldn't be locked exclusively
  101.     *   o file couldn't be unlocked
  102.     *   o file couldn't be closed
  103.     * 
  104.     * @throws PEAR_Error
  105.     * @access public
  106.     * @return mixed true on success or PEAR_Error
  107.     */
  108.     function save()
  109.     {
  110.         $content = '';
  111.         foreach ($this->_users as $user => $v){
  112.             $content .= $user . ':' . $v['passwd'];
  113.             if (isset($v['system']) && !empty($v['system'])) {
  114.                 $content .= ':' . $v['system'];
  115.             }
  116.             $content .= "\n";
  117.         }
  118.         return $this->_save($content);
  119.     }
  120.     
  121.     /** 
  122.     * Parse the CVS passwd file
  123.     *
  124.     * Returns a PEAR_Error if passwd file has invalid format.
  125.     * 
  126.     * @throws PEAR_Error
  127.     * @access public
  128.     * @return mixed true on success or PEAR_Error
  129.     */
  130.     function parse()
  131.     {
  132.         $this->_users = array();
  133.         foreach ($this->_contents as $line) {
  134.             $user = explode(':', $line);
  135.             if (count($user) < 2) {
  136.                 return PEAR::raiseError(
  137.                     FILE_PASSWD_E_INVALID_FORMAT_STR,
  138.                     FILE_PASSWD_E_INVALID_FORMAT
  139.                 );
  140.             }
  141.             @list($user, $pass, $system) = $user;
  142.             $this->_users[$user]['passwd'] = $pass;
  143.             if (!empty($system)) {
  144.                 $this->_users[$user]['system'] = $system;
  145.             }
  146.         }
  147.         $this->_contents = array();
  148.         return true;
  149.     }
  150.  
  151.     /**
  152.     * Add an user
  153.     *
  154.     * The username must start with an alphabetical character and must NOT
  155.     * contain any other characters than alphanumerics, the underline and dash.
  156.     * 
  157.     * Returns a PEAR_Error if:
  158.     *   o user already exists
  159.     *   o user or system_user contains illegal characters
  160.     * 
  161.     * @throws PEAR_Error
  162.     * @access public
  163.     * @return mixed true on success or PEAR_Error
  164.     * @param  string    $user           the name of the user to add
  165.     * @param  string    $pass           the password of the user tot add
  166.     * @param  string    $system_user    the systemuser this user maps to
  167.     */
  168.     function addUser($user, $pass, $system_user = '')
  169.     {
  170.         if ($this->userExists($user)) {
  171.             return PEAR::raiseError(
  172.                 sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
  173.                 FILE_PASSWD_E_EXISTS_ALREADY
  174.             );
  175.         }
  176.         if (!preg_match($this->_pcre, $user)) {
  177.             return PEAR::raiseError(
  178.                 sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
  179.                 FILE_PASSWD_E_INVALID_CHARS
  180.             );
  181.         }
  182.         @setType($system_user, 'string');
  183.         if (!empty($system_user) && !preg_match($this->_pcre, $system_user)) {
  184.             return PEAR::raiseError(
  185.                 sprintf(
  186.                     FILE_PASSWD_E_INVALID_CHARS_STR, 
  187.                     'System user ', 
  188.                     $system_user
  189.                 ),
  190.                 FILE_PASSWD_E_INVALID_CHARS
  191.             );
  192.         }
  193.         $this->_users[$user]['passwd'] = $this->generatePassword($pass);
  194.         $this->_users[$user]['system'] = $system_user;
  195.         return true;
  196.     }
  197.     
  198.     /**
  199.     * Verify the password of a certain user
  200.     *
  201.     * Returns a PEAR_Error if the user doesn't exist.
  202.     * 
  203.     * @throws PEAR_Error
  204.     * @access public
  205.     * @return mixed true if passwords equal, false ifthe don't or PEAR_Error
  206.     * @param  string    $user   user whose password should be verified
  207.     * @param  string    $pass   the plaintext password that should be verified
  208.     */
  209.     function verifyPasswd($user, $pass)
  210.     {
  211.         if (!$this->userExists($user)) {
  212.             return PEAR::raiseError(
  213.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  214.                 FILE_PASSWD_E_EXISTS_NOT
  215.             );
  216.         }
  217.         $real = $this->_users[$user]['passwd'];
  218.         return ($real === $this->generatePassword($pass, $real));
  219.     }
  220.     
  221.     /**
  222.     * Change the password of a certain user
  223.     *
  224.     * Returns a PEAR_Error if user doesn't exist.
  225.     * 
  226.     * @throws PEAR_Error
  227.     * @access public
  228.     * @return mixed true on success or PEAR_Error
  229.     */
  230.     function changePasswd($user, $pass)
  231.     {
  232.         if (!$this->userExists($user)) {
  233.             return PEAR::raiseError(
  234.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  235.                 FILE_PASSWD_E_EXISTS_NOT
  236.             );
  237.         }
  238.         $this->_users[$user]['passwd'] = $this->generatePassword($pass);
  239.         return true;
  240.     }
  241.     
  242.     /**
  243.     * Change the corresponding system user of a certain cvs user
  244.     *
  245.     * Returns a PEAR_Error if:
  246.     *   o user doesn't exist
  247.     *   o system user contains illegal characters
  248.     * 
  249.     * @throws PEAR_Error
  250.     * @access public
  251.     * @return mixed true on success or PEAR_Error
  252.     */
  253.     function changeSysUser($user, $system)
  254.     {
  255.         if (!$this->userExists($user)) {
  256.             return PEAR::raiseError(
  257.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  258.                 FILE_PASSWD_E_EXISTS_NOT
  259.             );
  260.         }
  261.         if (!preg_match($this->_pcre, $system)) {
  262.             return PEAR::raiseError(
  263.                 sprintf(
  264.                     FILE_PASSWD_E_INVALID_CHARS_STR, 
  265.                     'System user ', 
  266.                     $system_user
  267.                 ),
  268.                 FILE_PASSWD_E_INVALID_CHARS
  269.             );
  270.         }
  271.         $this->_users[$user]['system'] = $system;
  272.         return true;
  273.     }
  274.     
  275.     /**
  276.     * Generate crypted password
  277.     *
  278.     * @static
  279.     * @access public
  280.     * @return string    the crypted password
  281.     * @param  string    $pass   new plaintext password
  282.     * @param  string    $salt   new crypted password from which to gain the salt
  283.     */
  284.     function generatePassword($pass, $salt = null)
  285.     {
  286.         return File_Passwd::crypt_des($pass, $salt);
  287.     }
  288.     
  289. }
  290. ?>